package com.dempsey.modules.file.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.dempsey.common.core.domain.AjaxResult;
import com.dempsey.modules.file.service.FileService;
import com.dempsey.modules.file.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author Dempsey
 * Date 2022/5/20
 * Description
 */

@Api(value = "/file", tags = {"文件控制"})
@RestController
@RequestMapping("/file")
public class FileController {

    private final static Log log = LogFactory.get();

    @Autowired
    private FileService fileService;

    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "file", name = "multipartFile", value = "")})
    @ApiOperation(value = "文件上传", notes = "", httpMethod = "POST")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile multipartFile) throws Exception {
        log.info("请求参数: {}", multipartFile);

        File file = FileUtil.multipartFileToFile(multipartFile);

        String uploadFilePath = fileService.upload(file);

        return AjaxResult.success(uploadFilePath);
    }

    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "file", name = "multipartFile", value = "")})
    @ApiOperation(value = "头像上传", notes = "", httpMethod = "POST")
    @PostMapping("/uploadHeadImg")
    public AjaxResult uploadHeadImg(MultipartFile multipartFile) throws Exception {
        log.info("请求参数: {}", multipartFile);

        File file = FileUtil.multipartFileToFile(multipartFile);

        String uploadFilePath = fileService.uploadHeadImg(file);

        return AjaxResult.success(uploadFilePath);
    }

}
